/*
*    Phantom Test Driver v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/*
 * SuiteTestcase.java
 *
 * Created on December 7, 2005, 6:58 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package ptd_suite_objects;

import java.io.*;

import ptd.Main;
import ptd.PTDDisplay;
import ptd.PTDHTMLPane;
import ptd.PTDRemoteMachine;
import ptd.PTDRemoteRunner;
import ptd.PTDScriptComm;
import ptd.PTDScriptRunner;
import ptd.PTDSuiteDisplay;

/**
 *
 * @author John
 */
public class SuiteTestcase extends SuiteBase {
    
    public String myFile;
    public String myExec;
    public String myArguments;
    //public String myOutput;
    //public String myBase;
    
    public int UsePhantom;
    //public int CompareOutput;

    
    /** Creates a new instance of SuiteTestcase */
    public SuiteTestcase() {
        myType = TESTCASE_TYPE;
        UsePhantom = 1;
        //CompareOutput = 0;
        myFile = "";
        myExec = "";
        myArguments = "";
        //myOutput = "";
        //myBase = "";
    }
    
    public SuiteTestcase(SuiteTestcase Other){
        super(Other);
        myType = Other.myType;
        UsePhantom = Other.UsePhantom;
        //CompareOutput = Other.CompareOutput;
        myFile = Other.myFile;
        myExec = Other.myExec;
        myArguments = Other.myArguments;
        //myOutput = Other.myOutput;
        //myBase = Other.myBase;
    }
    
    
    // --- OVERRIDEABLES -----
    
    public boolean isEqualTo(SuiteBase SOther){
        SuiteTestcase Other = (SuiteTestcase)SOther;
        
        if(myFile.compareTo(Other.myFile) != 0){
            return false;
        }
        if(myExec.compareTo(Other.myExec) != 0){
            return false;
        }
        if(myArguments.compareTo(Other.myArguments) != 0){
            return false;
        }

        //if(myOutput.compareTo(Other.myOutput) != 0){
        //    return false;
        //}
        //if(myBase.compareTo(Other.myBase) != 0){
        //    return false;
        //}

        if(UsePhantom != Other.UsePhantom){
            return false;
        }
        
        //if(CompareOutput != Other.CompareOutput){
        //    return false;
        //}
        return super.isEqualTo(SOther);
    }
    
    protected int do_load(String s){
//        int iPos = s.indexOf(mySepChar);
//        if(iPos < 0){
//            return 0;
//        }
        return 1;
    }
    
    protected int do_load(FileInputStream f){
        
        super.do_load(f);
        
        Integer anInt = new Integer(0);
        anInt = loadInt(f);
        if(anInt == null) return 0;
        UsePhantom = anInt.intValue();

        //anInt = loadInt(f);
        //if(anInt == null) return 0;
        //CompareOutput = anInt.intValue();

        
        String s;
        // Get File
        s = loadString(f);
        if(s == null) return 0;
        myFile = s;

        // Get Exec
        s = loadString(f);
        if(s == null) return 0;
        myExec = s;

        // Get Arguments
        s = loadString(f);
        if(s == null) return 0;
        myArguments = s;
        
        
        // Get Output
        //s = loadString(f);
        //if(s == null) return 0;
        //myOutput = s;
        
        // Get Base
        //s = loadString(f);
        //if(s == null) return 0;
        //myBase = s;
       

        return 1;
    }

    protected int do_save(String s){
        
        return 1;
    }
    
    protected int do_save(FileOutputStream f){

        super.do_save(f);
        
        Integer anInt = new Integer(UsePhantom);
        if(saveInt(f, anInt) == false) return 0;
            
        //anInt = new Integer(CompareOutput);
        //if(saveInt(f, anInt) == false) return 0;
            
        if(saveString(f, myFile) == false) return 0;
        if(saveString(f, myExec) == false) return 0;
        if(saveString(f, myArguments) == false) return 0;
        //if(saveString(f, myOutput) == false) return 0;
        //if(saveString(f, myBase) == false) return 0;

        return 1;
    }
    
    public void initRun(PTDDisplay inDisp){
        inDisp.startTestcase(myName);
    }

    public int run(PTDDisplay inDisp, PTDScriptComm inComm){
        //Main.debug_out("SuiteTestcase: Running " + myName); 
        if(UsePhantom > 0){
            PTDScriptRunner aRunner = new PTDScriptRunner();
            
            if(aRunner.loadFile(myFile) == 0){
                inDisp.displayError("File [" + myFile + "] does not exist.\n");
            }else{
                aRunner.setArguments(myArguments);
                aRunner.setOutput(inDisp);
                aRunner.setComm(inComm);        
                aRunner.run(true, false, false);
            }
        }else{
            String sCmd = myExec + " " + myFile;
            if(myArguments.length() > 0){
                sCmd = sCmd + " " + myArguments;
            }
            inDisp.displayFormatted("Executing Non-Phantom Script: ["+sCmd+"]");
            File F = new File(myExec);
            try{
                if (F.exists()){
                    Runtime rt = Runtime.getRuntime();
                
                    Process proc = rt.exec(sCmd);
                    
                    InputStream is = proc.getInputStream();
                    
                    int len;
                    byte buf[] = new byte[1000];
                    try {
                        while( (len = is.read(buf)) != -1 ) {
                            String str = new String(buf,0,0,len);
                            inDisp.display(str);
                        }
                    }  catch (IOException e) { }
                    
                    //proc.waitFor();
                    inDisp.displayFormatted("  Success... exit code ["+proc.exitValue()+"]");
                    proc.destroy();
                }else{
                    inDisp.display("** PTD ERROR: Could not find ["+myExec+"]");
                }
            }catch(Exception IOEx){
                Main.debug_out(IOEx.getMessage());
            }
        }
        return 1;
    }

    public int runRemote(PTDDisplay inDisp, PTDScriptComm inComm, PTDRemoteMachine Machine){       
        if(Machine.runState == false){
            return 0;
        }
        if(UsePhantom > 0){
            PTDRemoteRunner aRunner = new PTDRemoteRunner();
            aRunner.setOutput(inDisp);
            aRunner.setComm(inComm);
            aRunner.setMachine(Machine);
            aRunner.setScriptPath(myFile);
            aRunner.setArguments(myArguments);
            Machine.setCurrentCase(myName + "   [Script: " + myFile + "]");
            aRunner.run(true, false, false);
            Machine.setCurrentCase("None");
            //PTDScriptRunner aRunner = new PTDScriptRunner();
            //if(aRunner.loadFile(myFile) == 0){
            //    inDisp.displayError("File [" + myFile + "] does not exist.\n");
            //}else{
            //    aRunner.setOutput(inDisp);
            //    aRunner.setComm(inComm);        
            //    aRunner.run(true, false, false);
            //}
        }else{
            inDisp.displayFailure("PTD can not run non-phantom scripts remotely");
        }
        return 1;
    }    
    
    public SuiteBase duplicate(){
        return (SuiteBase)(new SuiteTestcase(this));
        /*
        SuiteTestcase Ret = new SuiteTestcase(); //= //(SuiteTestcase)super.duplicate();

        Ret.myChildrenCount = myChildrenCount;
        Ret.myDescription = myDescription;
        Ret.myPreprocessor = myPreprocessor;
        Ret.myPostprocessor = myPostprocessor;
        Ret.myID = myID;
        Ret.myName = myName;
        Ret.myRunSelectState = myRunSelectState;
        Ret.myType = myType;
        
        Ret.CompareOutput = CompareOutput;
        Ret.UsePhantom = UsePhantom;
        Ret.myBase = myBase;
        Ret.myExec = myExec;
        Ret.myFile = myFile;
        Ret.myOutput = myOutput;
        return (SuiteBase)Ret;
         */
    }
    
}
